from pkgr.tests.test_pkgr import Test_Pkgr
import os
import logging
import solv


logger = logging.getLogger('pkgr.test.test_pkgr')
class Test_Pkg_Basic(Test_Pkgr):
        
    def test_erase(self):
        ''' Test erasing a package. '''
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('install', ['a'])
        pkgr.set_jobs(jobs)
        
        for job in jobs:
            job.how |= solv.Job.SOLVER_INSTALL
            
        solver = None
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        del solver
        
        self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'a')))
        self.dump_install_root()
        
        ######################
        
        self.load_new_pkgr()
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('erase', ['a'])
        
        print '!!!!!!!', jobs
        pkgr.set_jobs(jobs)
        
        for job in jobs:
            #job.how |= solv.Job.SOLVER_UPDATE
            job.how |= solv.Job.SOLVER_ERASE
            
        solver = None
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        #del solver
        
        #self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.assertTrue(not os.path.exists(os.path.join(self.installroot, 'a')))
        self.dump_install_root()
        
     
